package singleton;

/**
 * 懒汉式
 * 唯一缺点: 存在线程不安全问题
 * 可以通过synchronized来解决,但是效率也下来了,锁优化
 */
public class Mgr04 {

    private static Mgr04 INSTANCE;

    private Mgr04() {};

    public static Mgr04 getInstance() {
        if(null == INSTANCE){
            synchronized (Mgr04.class){     //但是此处这样写还是会存在问题
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                INSTANCE = new Mgr04();
            }
        }
        return INSTANCE;
    }

    public void m() {
        System.out.println("m");
    }

    public static void main(String[] args) {
        for(int i = 0; i < 100; i++){
            new Thread(() -> System.out.println(Mgr04.getInstance().hashCode())).start();
        }
    }

}
